package weibo.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;
import com.nostra13.universalimageloader.core.ImageLoader;


import java.util.ArrayList;
import java.util.List;

import managers.UIHandler;
import model.Blog;
import weibo.acitivity.BokeActivity2;
import weibo.acitivity.MyPhotoActivity;

/**
 * Created by Administrator on 2016/4/1.
 */
public class BokeAdapter extends BaseAdapter {

    List<Blog> blogs = new ArrayList<Blog>();
    int[] ids = new int[]{R.id.id_img0, R.id.id_img1,R.id.id_img2,R.id.id_img3,R.id.id_img4,R.id.id_img5};


    public void setBlogs(List<Blog> blogs) {
        this.blogs = blogs;
    }

    @Override
    public int getCount() {
        return blogs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = View.inflate(MyApplication.share(), R.layout.item_blog,null);
            viewHolder.image_avar = (ImageView) convertView.findViewById(R.id.id_img_avar);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.id_tv_name);
            viewHolder.tv_text = (TextView) convertView.findViewById(R.id.id_tv_text);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
            viewHolder.tv_class = (TextView) convertView.findViewById(R.id.id_class);
            addAllImgs(viewHolder,convertView,ids);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Blog blog = blogs.get(position);
        viewHolder.tv_name.setText(blog.getUser_Name()+"");
        viewHolder.tv_time.setText(blog.getBlog_Time());
        viewHolder.tv_text.setText(blog.getBlog_Text());


        if (blog.getBlog_Text() == null || blog.getBlog_Text().isEmpty())
        {
            viewHolder.tv_text.setVisibility(View.GONE);
        }
        else
        {
            viewHolder.tv_text.setVisibility(View.VISIBLE);
        }

        hideAllImgs(viewHolder);
        if (blog.getImage_Num() != 0)
        {
            String[] filenames = blog.getImage_Set().split(";");
            int j = 0;
            for (int i=0;i<filenames.length;i++)
            {
                if (filenames[i].isEmpty()) continue;
                String ip = MyApplication.share().getString(R.string.IP);
                final int mPosition = position;
                final int mJ = j;
                viewHolder.imageViews.get(j).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("位置","position:"+mPosition+",图片位置:"+mJ);
                        Intent intent = new Intent(UIHandler.uiHandler.getActivityByName(BokeActivity2.class.getSimpleName()), MyPhotoActivity.class);
                        intent.putExtra("blog",blog);
                        intent.putExtra("position",mJ);
                        UIHandler.uiHandler.getActivityByName(BokeActivity2.class.getSimpleName()).startActivity(intent);

                    }
                });
                ImageLoader.getInstance().displayImage("http://"+ip+"/Blog/DownloadFile?name="+filenames[i]+"_sm",viewHolder.imageViews.get(j++),MyApplication.options);

            }
            showImgs(viewHolder,blog.getImage_Num());
        }

        if (blog.getBlog_class() == null)
        {
            viewHolder.tv_class.setText("未分类");
        }
        else
        {
            viewHolder.tv_class.setText(blog.getBlog_class());
        }

        return convertView;
    }

    // 显示图片
    private void showImgs(ViewHolder viewHolder, int image_num) {
        for (int i=0;i<image_num;i++)
        {
            viewHolder.imageViews.get(i).setVisibility(View.VISIBLE);
        }
    }

    public void addAllImgs(ViewHolder viewHolder,View convertView,int...ids)
    {
        for(int id:ids)
        {
            viewHolder.imageViews.add((ImageView) convertView.findViewById(id));
        }
    }

    public void hideAllImgs(ViewHolder viewHolder)
    {
        for (ImageView imageView:viewHolder.imageViews)
        {
            imageView.setVisibility(View.GONE);
        }
    }

    class ViewHolder
    {
        public ImageView image_avar;
        public TextView tv_name;
        public TextView tv_time;
        public TextView tv_text;
        public List<ImageView> imageViews = new ArrayList<ImageView>();
        public TextView tv_class;
    }

}
